#!/usr/bin/python2.5

import mailbox
import time
import datetime
from meet_day_script import calc_meet_date

y,m,d = calc_meet_date(int(time.strftime('%Y')),int(time.strftime('%m')))
    
if datetime.date.today() > datetime.date(y,m,d):
	y,m,d = calc_meet_date(None,m+1)

try:
	open("/home/ilughyd/agendas/agenda_%d%d%d.txt" %(y,m,d),"r")
except IOError:
	mail_dir=mailbox.Maildir("/home/ilughyd/Maildir",None)
	mails = mail_dir.values()

	for mail in mails:
		if mail['From']=='"Thejaswi Puthraya" <thejaswi.puthraya@gmail.com>' and mail['Subject']=='[LUG]Agenda':
			if not mail.is_multipart():
				agenda_text = mail.get_payload(decode=True)		
				f=open("/home/ilughyd/agendas/agenda_%d%d%d.txt" %(y,m,d),"w")
				f.write(agenda_text)
				f.close()

